package servlet.gc;


import pojo.Student;
import service.StudentService;
import service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/getStudentList")
public class StudentListServlet extends HttpServlet {
    StudentService studentService = new StudentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> students = null;
        //模糊查询
        String stuName = req.getParameter("stuName");
        String stuNo= req.getParameter("stuNo");
//        Integer sex = Integer.parseInt(req.getParameter("sex"));
        String sex = req.getParameter("sex");
        int sexi = -1;
        if (sex!=null&&sex!=""){
            sexi = Integer.parseInt(sex);
        }
        //分页数据
        //页码值
        String pageIndex = req.getParameter("pageIndex");
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);



//        System.out.println(stuName);
//        System.out.println(stuNo);
        try {
            students = studentService.selectAll(stuName,stuNo,sexi,index,5);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //获取总页数
        int totalPage = 0;
        try {

            totalPage = studentService.total(stuName,stuNo,sexi)%5>0?
                    studentService.total(stuName,stuNo,sexi)/5+1:
                    studentService.total(stuName,stuNo,sexi)/5;
            req.setAttribute("total",studentService.total(stuName,stuNo,sexi));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
//        System.out.println(students);
        req.setAttribute("stuList",students);
        req.setAttribute("stuName",stuName);
        req.setAttribute("stuNo",stuNo);
        req.setAttribute("sex",sexi);
        req.setAttribute("pageIndex",index);
        req.setAttribute("totalPage",totalPage);
        req.getRequestDispatcher("/Educational/student/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
